/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.opt.genetic.impl.prmopt;

import java.util.ArrayList;
import org.baatar.opt.genetic.GeneticIterationListener;
import org.baatar.opt.genetic.GeneticOptimizer;
import org.baatar.opt.genetic.comparator.MinimizationComparator;
import org.baatar.opt.genetic.crossover.UniformCrossover;
import org.baatar.opt.genetic.selection.BinaryTournamentSelection;

/**
 *
 * @author Ba
 */
public class main {
    public static void main(String args[]) throws Exception {

        ArrayList<ParamInfo> paramInfos = new ArrayList<ParamInfo>();

        paramInfos.add(new ParamInfo("x", -1000, 1000, 16));
        paramInfos.add(new ParamInfo("y", -1000, 1000, 16));

        ChromosomeFactory chromosomeFactory = new ChromosomeFactory(paramInfos,
                                                                    "return (x-50)*(x-50)+(y-80)*(y-80);");

        GeneticOptimizer<Boolean> go = new GeneticOptimizer<Boolean>();

        go.setMaxNumOfIterations(500);
        go.setMaxNumOfIterationsWOProgress(200);
        go.setPopulationSize(100);
        go.setNumOfChildren(20);
        go.setNumOfEliteChromosomes(4);
        go.setMutationRate(0.01);
        go.setGreedRate(0.03);
        go.setAllowDublicateChromosomes(false);

        go.setComparator(new MinimizationComparator<Boolean>());
        go.setChromosomeFactory(chromosomeFactory);
        go.setSelector(new BinaryTournamentSelection<Boolean>());
        go.setCrossoverOperator(new UniformCrossover<Boolean>());
        go.setMutator(new Mutator());

        go.setGeneticIterationListener(new GeneticIterationListener<Boolean>() {
            @Override
            public void onProgress(GeneticOptimizer<Boolean> go, int iteration, String info) {
                System.out.println(iteration + " - " + info);
            }

            @Override
            public void onIterate(int iteration, int numOfIterationsPassedWOProgress, int maxNumOfIterations) {
            }
        });

        try {
            if (go.initialize())
                go.start();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
